using System;

namespace MetaView.Mvvm;

/// <summary>
/// Represents an action that can be executed when an event is raised.
/// </summary>
/// <typeparam name="TSender"></typeparam>
/// <typeparam name="TE"></typeparam>
public class EventAction<TSender, TE> : IEventAction
{
    private readonly Action<TSender, TE> m_action;

    /// <summary>
    /// Initializes a new instance of the <see cref="EventAction{TSender, TE}"/> class.
    /// </summary>
    /// <param name="eventName"></param>
    /// <param name="action"></param>
    public EventAction(string eventName, Action<TSender, TE> action)
    {
        this.EventName = eventName;
        this.m_action = action;
    }

    /// <summary>
    /// Gets the name of the event that this action is associated with.
    /// </summary>
    public string EventName { get; }

    /// <summary>
    /// Executes the action when the event is raised.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Event(TSender sender, TE e)
    {
        this.m_action?.Invoke(sender, e);
    }
}